<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Cms_Themes
 *
 * @author hassanali
 */
class themes {

    public function Cms_themes() {
        $CI = & get_instance();
        if ($CI->agent->is_mobile()) {
            return 'template/mobile';
        } else {
            return 'template/default';
        }
    }

    public function render($file = NULL, $data = array(), $boolen = false, $contaner = 'index.php') {
        $CI = & get_instance();
        if (!empty($file)) {
            $data['file_contant'] = $file;
        } else {
            $data['file_contant'] = '404';
        }
        if (in_array(ADMIN_FOLDER, get_current_url())) {
            $CI->load->view(ADMIN_FOLDER . '/index.php', $data, $boolen);
        } else {
            $CI->load->view($this->Cms_themes() . '/' . $contaner, $data, $boolen);
        }
    }

    public function set_message($data = array('url' => '', 'type' => 'success', 'message' => 'message_operation_success')) {


        $CI = & get_instance();
        // if empty data
        if (!array_key_exists('url', $data)) {
            $data['url'] = '';
        }
        if (!array_key_exists('type', $data)) {
            $data['type'] = 'success';
        }
        if (!array_key_exists('message', $data)) {
            $data['message'] = 'message_operation_success';
        }


        $data_session['type'] = $data['type'];
        $data_session['themes_message'] = $data['message'];
        $last_url = str_replace('index.php', '', $CI->uri->uri_string());
        if ($last_url != ADMIN_FOLDER . '/user/login' || $last_url != ADMIN_FOLDER . '/user/logout') {
            $data_session['back_page'] = $last_url;
        }

        if (strpos($data['url'], 'http') === FALSE) {
            if (in_array(ADMIN_FOLDER, get_current_url())) {
                $data['url'] = base_url() . ADMIN_FOLDER . '/' . $data['url'];
            } else {
                $data['url'] = base_url() . $data['url'];
            }
        }

        $CI->session->set_userdata($data_session);
        redirect($data['url']);
        exit();
    }

    public function display_message() {
        $CI = & get_instance();

        $data['message'] = $CI->session->userdata('themes_message');
        $data['type'] = $CI->session->userdata('type');

        if (in_array($data['type'], array('success', 'error', 'validation_error'))) {
            $file_contant = $data['type'];
        }

        if (!empty($data['message']) && isset($file_contant)) {
            if ($CI->uri->segment(1) == ADMIN_FOLDER) {
                $CI->load->view(ADMIN_FOLDER . '/message/' . $file_contant . '.php', $data);
            } else {
                $CI->load->view($this->Cms_themes() . '/message/' . $file_contant . '.php', $data);
            }
        }
        $CI->session->set_userdata(array('themes_message' => '', 'type' => ''));
    }

}

?>
